public class Solution861 {
    public int matrixScore(int[][] grid) {
        int n=grid.length,m=grid[0].length;
        for (int i=0;i<n;i++){
            if (grid[i][0]==0){
                for (int j=0;j<m;j++){
                    grid[i][j]=1-grid[i][j];
                }
            }
        }
        for (int j=0;j<m;j++){
            int z=0,o=0;
            for (int i=0;i<n;i++){
                if (grid[i][j]==1){
                    o++;
                }else {
                    z++;
                }
            }
            if (z>o){
                for (int i=0;i<n;i++){
                    grid[i][j]=1-grid[i][j];
                }
            }
        }
        int ans=0;
        for (int i=0;i<n;i++){
            int t=0;
            for (int j=0;j<m;j++){
                t=t*2+grid[i][j];
            }
            ans=ans+t;
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution861().matrixScore(new int[][]{{0,0,1,1},{1,0,1,0},{1,1,0,0}}));
    }
}
